<template>
  <div ref="content" class="content">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px" :class="{header:needTagsView && fixedHeader}">
      <el-form-item label="缴费订单" prop="userName">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入要搜索的内容"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="tableData.filter(data => !search || data.houseResidentialQuarters.toLowerCase().includes(search.toLowerCase()))"
      tooltip-effect="dark"
      height="600px"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        prop="orderId"
        label="订单ID"
        width="120"
      />
      <el-table-column
        prop="houseId"
        label="房屋编号"
        width="120"
      />
      <el-table-column
        prop="orderCost"
        label="订单消费(单位:元)"
        width="200"
      />
      <el-table-column
        prop="orderType"
        label="类型"
      >
        <template slot-scope="Scope">
          <el-tag v-show="Scope.row.orderType==null" type="danger">未选择类型</el-tag>
          <el-tag v-show="Scope.row.orderType==0">水费</el-tag>
          <el-tag v-show="Scope.row.orderType==1">物业费</el-tag>
          <el-tag v-show="Scope.row.orderType==2">公共设施维修费</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderState"
        label="状态"
      >
        <template slot-scope="Scope">
          <el-tag v-show="Scope.row.orderState==0" type="danger">未缴费</el-tag>
          <el-tag v-show="Scope.row.orderState==1" type="warning">已缴费</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="creatTime"
        label="创建时间"
      />
      <el-table-column
        fixed="right"
      >
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="查看详细数据" placement="bottom-end">
            <el-button
              type="text"
              size="small"
              @click.native.prevent="lookUp(scope.$index, tableData)"
            >
              查看
            </el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="修改数据" placement="bottom-end">
            <el-button
              type="text"
              size="small"
              @click.native.prevent="update(scope.$index, tableData)"
            >
              修改
            </el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除此条数据" placement="bottom-end">
            <el-button
              type="text"
              size="small"
              @click.native.prevent="deleteRow(scope.$index, tableData)"
            >
              删除
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="text-align:center"
      background
      :page-sizes="[20, 40, 80, 100, 200]"
      :page-size="20"
      layout="sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- dialog 房屋订单弹窗 -->
    <el-dialog title="房屋订单信息" :visible.sync="dialogVisible" :before-close="formClose">
      <el-form :model="form" label-position="top">
        <el-row :gutter="20">
          <el-col v-show="!insertState" :span="12">
            <el-form-item label="订单ID">
              <el-input v-model="form.orderId" autocomplete="off" readonly maxlength="15" />
            </el-form-item>
          </el-col>
          <el-col :span="insertState?24:12">
            <el-form-item label="房屋编号">
              <el-input v-model="form.houseId" autocomplete="off" :readonly="!insertState" maxlength="20" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="订单类型">
              <el-radio-group v-model="form.orderType" :disabled="!formState">
                <el-radio :label="0">水费</el-radio>
                <el-radio :label="1">物业费</el-radio>
                <el-radio :label="2">公共设施维修费</el-radio>
              </el-radio-group>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-select v-model="form.orderState" placeholder="请选择状态" :disabled="!formState">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="订单金额">
              <el-input v-model="form.orderCost" autocomplete="off" :readonly="!formState" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单描述">
              <el-input v-model="form.orderDescribe" autocomplete="off" :readonly="!formState" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div v-show="formState" slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button v-show="!insertState" type="primary" @click="updateConfirm">确 定</el-button>
        <el-button v-show="insertState" type="primary" @click="insertConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getAllHouseOrder, updateHouseOrder, deleteHouseOrder, insertHouseOrder } from '@/api/houseorder'

export default {
  data() {
    return {
      // 是否是加载状态转圈
      loading: false,
      // 总数据
      total: 0,
      // 当前页数据数量
      pageSize: 20,
      // 当前页数
      pageCurrent: 1,
      // 当前显示的表单数据
      tableData: [],
      // 左侧选择框的选中列表
      multipleSelection: [],
      // 查看修改弹窗显示状态
      dialogVisible: false,
      // 进度弹窗的显示隐藏状态
      dialogProgressVisible: false,
      // 当前步骤条状态号
      progressStep: 1,
      // 当前进度的获取状态 控制骨架屏的显隐
      progressState: false,
      // 弹窗显示数据深拷贝的
      form: {},
      // 表单控制按钮组的显隐及控制权
      formState: false,
      // 当前操作的index
      indexCurrent: 0,
      // 当前操作的index
      rowsCurrent: {},
      // 是否是增加状态
      insertState: false,
      // 搜索数据
      search: '',
      // 是否在操作列上方显示搜索
      showSearch: true,
      // 搜索的参数
      queryParams: {
        name: undefined
      },
      // 事件跟踪数据
      processTackings: [],
      // 下拉框状态内容
      options: [{
        value: '0',
        label: '未缴费'
      }, {
        value: '1',
        label: '已缴费'
      }]
    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView,
      fixedHeader: state => state.settings.fixedHeader
    })
  },
  created() {
    this.init()
  },
  methods: {
    // 初始化函数
    init() {
      this.loading = true
      getAllHouseOrder(this.pageSize, this.pageCurrent).then(resp => {
        this.total = resp.obj.total
        this.tableData = resp.obj.list
        this.loading = false
      })
    },
    // 左侧选择框
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 删除行
    deleteRow(index, rows) {
      this.deleteConfirm(index, rows)
    },
    // 查看信息
    lookUp(index, rows) {
      this.indexCurrent = index
      this.rowsCurrent = rows
      this.form = JSON.parse(JSON.stringify(rows[index]))
      this.form.orderState = this.options[rows[index].orderState].value
      this.formState = false
      this.dialogVisible = true
      this.insertState = false
    },
    // 修改信息
    update(index, rows) {
      this.indexCurrent = index
      this.rowsCurrent = rows
      this.form = JSON.parse(JSON.stringify(rows[index]))
      this.form.orderState = this.options[rows[index].orderState].value
      this.formState = true
      this.dialogVisible = true
      this.insertState = false
    },
    // 弹窗表单关闭回调函数
    formClose() {
      this.dialogVisible = false
    },
    // 进度弹窗关闭回调函数
    progressClose() {
      this.dialogProgressVisible = false
    },
    //  更新二次确认弹窗
    updateConfirm() {
      this.$confirm('确定要更新数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateHouseOrder(this.form).then(rsp => {
          if (rsp.result) {
            this.$message({
              type: 'success',
              message: '更新成功'
            })
            this.init()
          } else {
            this.$notify({
              title: '操作失败',
              message: '更新此条订单数据失败，请联系管理员',
              type: 'warning'
            })
          }
          this.dialogVisible = false
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
        this.formState = false
      })
    },
    // 增加二次确认
    insertConfirm() {
      console.log('wocao')
      this.$confirm('确定要增加此条数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        insertHouseOrder(this.form).then(rsp => {
          if (rsp.result) {
            this.$message({
              type: 'success',
              message: '添加成功'
            })
            this.init()
          } else {
            this.$notify({
              title: '操作失败',
              message: '添加此条订单数据失败，请联系管理员',
              type: 'warning'
            })
          }
          this.dialogVisible = false
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
        this.formState = false
      })
    },
    // 删除二次确认弹窗
    deleteConfirm(index, rows) {
      this.$confirm('确定要删除此条订单数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteHouseOrder(rows[index].orderId).then(rsp => {
          if (rsp.result) {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.dialogVisible = false
            rows.splice(index, 1)
          } else {
            this.$notify({
              title: '操作失败',
              message: '删除订单失败，请联系管理员',
              type: 'warning'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },

    // 改变分页数量
    handleSizeChange(val) {
      this.loading = true
      this.pageCurrent = 1
      getAllHouseOrder(val, 1).then(resp => {
        this.total = resp.obj.total
        this.tableData = resp.obj.list
        this.loading = false
      })
    },
    // 换页
    handleCurrentChange(val) {
      this.loading = true
      getAllHouseOrder(this.pageSize, val).then(resp => {
        this.total = resp.obj.total
        this.tableData = resp.obj.list
        this.loading = false
      })
    },
    // 增加角色
    handleAdd() {
      this.form = {}
      this.insertState = true
      this.dialogVisible = true
      this.formState = true
    },
    // 更新角色信息
    handleUpdate() {

    },
    // 删除角色信息
    handleDelete() {

    },
    // 搜索按钮操作
    handleQuery() {

    },
    // 重置按钮操作
    resetQuery() {
      this.queryParams.name = ''
    }
  }
}
</script>

<style>
.content {
   margin: 20px;
}
.header {
   margin-top: 54px;
}
.tinymacClass {
  position: relative;
  left: 0;
  top: 0;
  opacity: 0.5;
  width: 100%;
  height: 100%;
  background: #000;
  z-index: 998;
  overflow: auto;
  pointer-events: none;
}
</style>
